import { config } from '../config.js';
import { Common } from '../common/common.js';
import { MyModel } from '../../../modules/my.js';

const app = getApp();
const common = new Common();
const my = new MyModel();

Page({
  data: {
    imageUrl: null,
    histories: [], // 工作经历
    experiences: [], // 项目经验
    educations: [], // 教育经历
    sexArray: config.data.sexArray, // 性别模板
    experience: config.data.experience,  // 工作经验模板
    joinstauts: config.data.joinstauts, // 求职状态模板
    edu: config.data.edu, // 最高学历模板
    politics: config.data.politics, // 政治面貌模板
    name: null, // 姓名
    phone: null, // 电话号码
    sex: null,  // 性别
    birthday: null, // 出生日期
    worddate: null, // 工作经验
    jobStatus: null, // 求职状态
    maxedu: null, // 最高学历
    specialty: null, // 专业
    nativeplace: null, // 籍贯
    address: null, // 现住地址
    politic: null, // 政治面貌
    email: null, // 邮箱
    evaluation: '这个人很懒什么也没留下！', // 自我评价
    loading: false
  },
  // 查看图片
  viewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: [e.currentTarget.dataset.url]
    });
  },
  // 把简历存储到data中
  saveUserResume(res) {
    if (res) {
      console.log(res)
      this.setData({
        imageUrl: res['heardImage'],
        name: res['name'],
        phone: res['telphone'],
        sex: res['sex'],
        birthday: res['birthday'],
        worddate: res['workAge'],
        jobStatus: res['jobStatus'],
        maxedu: res['education'],
        specialty: res['specialty'],
        nativeplace: res['nativePlace'],
        address: res['hopeWorkAddress'],
        politic: res['politicsStatus'],
        email: res['email'],
        evaluation: res['selfDesc'],
        experiences: res['experiences'],
        histories: res['histories'],
        educations: res['educations']
      })
    }
  },
  // 提示用户创建 简历
  isCreateResume (res) {
    if (res.resume == null) {
      wx.showModal({
        title: '提示',
        content: '请先创建简历',
        success: res => {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/my/createresume/index',
            })
          } else if (res.cancel) {
            console.log('这个用户很懒不想创建简历')
          }
        }
      })
      this.setData({
        loading: true
      })
    }else {
      this.saveUserResume(res.resume);
      this.setData({
        loading: true
      })
    }
  },
  onShow: function() {
    app.isAuthize()
    .then(res => {
      if(res) {
        my.getResumes()
        .then(res => {
          this.isCreateResume(res.data)
        })
      }else {
        this.setData({
          loading: true
        })
      }
    })
  }
})